/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


public class NacionalParsing {


    public static void main(String[] args) {
        
        try {
            
            Scanner fileIn = new Scanner ( new FileReader ("resultados_nacional.txt") );
            PrintWriter fileOut = new PrintWriter( new FileWriter("archivoNacional.txt") );
        
            
            String s = "";
            System.out.println("Procesando datos...");
            
            
            int lineas = 0;
            while (fileIn.hasNext()){
                
                lineas++;
                s = fileIn.nextLine();
                StringTokenizer st = new StringTokenizer(s);
                
                ArrayList<String> A = new ArrayList<String>();
                
                while ( st.hasMoreTokens()){
                   A.add( st.nextToken() );
                }
                
                //Output
                for ( int i = 0; i < A.size(); i++ )
                    System.out.print( A.get(i) + " " );
                
                
                /*
                 * Nombre entidad y clave
                 * 
                 ******************************************/
                
                
                //posicion a partir de la cual empieza el nombre de la escuela
                int ini = 2;
                
                //Parsing de los nombres de los estados
                if (  A.get(0).compareTo("BAJA") == 0  ){
                    //Procesar las baja californias
                    if ( A.get(2).compareTo("SUR") == 0 ){
                        //es baja california sur
                        fileOut.print( A.get(0) + "\\" + A.get(1) + "\\" + A.get(2) + " " + A.get(3) + " ");
                        ini = 4;
                    }
                    else{
                        //es baja california
                        fileOut.print( A.get(0) + "\\" + A.get(1) + " " + A.get(2) + " " );
                        ini = 3;
                    }
                }
                else if ( A.get(0).compareTo("DISTRITO") == 0 || A.get(0).compareTo("NUEVO") == 0 || A.get(0).compareTo("QUINTANA") == 0 ){
                   //entidades de dos palabras
                   fileOut.print( A.get(0) + "\\" + A.get(1) + " " + A.get(2) + " " );
                   ini = 3; 
                }
                else if ( A.get(0).compareTo("SAN") == 0 ){
                    //San Luis Potosi
                    fileOut.print( A.get(0) + "\\" + A.get(1) + "\\" + A.get(2) + " " + A.get(3) + " ");
                        ini = 4;
                }
                else{
                    //entidades de una palabra
                    fileOut.print( A.get(0) + " " + A.get(1) + "  ");    
                }
                
                
                /*
                 * 
                 * Nombre escuela
                 ***********************/
                
                
                for (int i = ini; i < A.size() - 12; i++){
                  if (i != A.size()-13)
                    fileOut.print(A.get(i) + "\\" );
                  else
                    fileOut.print(A.get(i));  
                }
                
                
                /*
                 * Promedios
                 * 
                 * 
                 **************************************/
                
                double promedioSexto = Double.parseDouble( A.get(A.size()-1) )
                                        + Double.parseDouble( A.get(A.size()-2 ) )
                                        + Double.parseDouble(A.get(A.size()-3 ) );
                promedioSexto /= 3;
                
                
                double promedioQuinto = Double.parseDouble( A.get(A.size()-4) )
                                        + Double.parseDouble( A.get(A.size()-5 ) )
                                        + Double.parseDouble(A.get(A.size()-6 ) );
                promedioQuinto /= 3;
                
                double promedioCuarto = Double.parseDouble( A.get(A.size()-7) )
                                        + Double.parseDouble( A.get(A.size()-8 ) )
                                        + Double.parseDouble(A.get(A.size()-9 ) );
                promedioCuarto /= 3;
                
                double promedioTercero = Double.parseDouble( A.get(A.size()-10) )
                                        + Double.parseDouble( A.get(A.size()-11 ) )
                                        + Double.parseDouble(A.get(A.size()-12 ) );
                promedioTercero /= 3;
                
                double promedioGeneral = promedioTercero + promedioCuarto + promedioQuinto + promedioSexto;
                promedioGeneral /= 4;
                
                /*
                 * 
                 * 
                 * 
                 * 
                 **************************************/
                
                Formatter formatter = new Formatter();
                formatter.format("%.3f", promedioGeneral);
                fileOut.println("  " + formatter.toString() );
                
                fileOut.flush();
                
                //A.clear();
                System.out.print("\n");
                //Runtime.getRuntime().gc();
                
            }
            
            System.out.println(lineas);
            
            //System.out.println( s );
        
            fileIn.close();
            fileOut.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(NacionalParsing.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    
     } //fin main
}
